package decision;

import hasthi.common.BaseContext;
import hasthi.common.constants.WSDMConstants.SystemState;
import hasthi.instrument.JavaInstrumentation;
import hasthi.metamodel.ResourceModel;
import hasthi.metamodel.resource.ManagedResource;
import hasthi.tools.TestingUtils;
import hasthi.tools.analyze.RulePerfSuite;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.xml.namespace.QName;

public class CoordinatorLoopOverhead {
    public static void main(String[] args) throws Exception {
        BaseContext baseConfigContext = new BaseContext();
        TestingUtils utils = new TestingUtils(baseConfigContext);

        RulePerfSuite.createRandomResources(100000, utils);
        System.out.println(JavaInstrumentation.getMemoryUsage());
        
        long tot = 0;
        for (int i = 0; i < 100; i++) {
            List<QName> resourcesToRemove = new ArrayList<QName>();
            long curretTime = System.currentTimeMillis();
            ResourceModel resourceModel = utils.getMixedResourceModel();
            Collection<QName> resourceNames = resourceModel.getResources();
            int mngerEpoch5 = 30000 * 5;
            int mngerEpoch2 = 30000 * 2;
            final String crashStateStr = SystemState.CrashedState.toString();
            final String stoppedStateStr = SystemState.StoppedState.toString();
            final String rapairedStateStr = SystemState.RepairedState.toString();
            for (QName resourceName : resourceNames) {
                ManagedResource resource = resourceModel.getResource(resourceName);
                long timeSinceLastUpdate = curretTime - resource.getLastUpdated();
                
                if(timeSinceLastUpdate > mngerEpoch2){
                    final String state = resource.getState();
                    if(state.equals(rapairedStateStr)){
                        resourcesToRemove.add(resourceName);
                    }else if(timeSinceLastUpdate > mngerEpoch5 
                            && (state.equals(crashStateStr) 
                                    || state.equals(stoppedStateStr))){
                        resourcesToRemove.add(resourceName);
                    }
                }
                
                //SystemState state = SystemState.valueOf(resource.getState());
                // boolean removedCrashedOrstopped = (state ==
                // SystemState.CrashedState || state ==
                // SystemState.StoppedState)
                // && (curretTime - resource.getLastUpdated()) > mngerEpoch5;
                // boolean removeRepaired = (state == SystemState.RepairedState)
                // && (curretTime - resource.getLastUpdated()) > mngerEpoch2;
                //        
//                 if (removedCrashedOrstopped || removeRepaired) {
//                 resourcesToRemove.add(resourceName);
//                 // }else{
//                 // systemStat.update(resource);
//                 }
            }
            long val = System.currentTimeMillis() - curretTime;
            System.out.println("Took " + val + "ms");
            tot = tot + val;

        }
        System.out.println("Done mean = " + tot / 100 + "ms");
        System.exit(0);
    }
}
